var express = require('express');
var router = express.Router();
var config = require('../config/db')    //加载数据库配置模块
var MongoClient = require('mongodb').MongoClient

/* 饰品中心 */
router.get('/jewelry', function(req, res, next) {
    //1.接受登录需要使用的参数
    var loginUser = req.body
    //2.连接数据库MongoDB
    MongoClient.connect(config.url, function (err, client) {
      if (err) {
        console.log('数据库连接失败', err)
        res.json({ code: 200, message: 'error', data: err })
      } else {
        //3.执行条件查询操作 db.find
        var db = client.db()
        var flag = false
        db.collection('products').find().toArray(function (err, result) {
          for (var i = 0; i < result.length; i++) {
            var pwd = result[i].password
            var orinalPwd = loginUser.userPwd
            flag = bcrypt.compareSync(orinalPwd, pwd)
            if (flag) {
              break
            }
          }
        })
      }
    })
  //2.读取数据文件
  var filePath = path.join(__dirname,'..','public','productsAll.json')
  fs.readFile(filePath,function(err,data){
    if(err){
      console.log('数据文件读取失败，',err)
    }else{
      var products = JSON.parse(data.toString()).data
      var result = []
      for(var i = 0 ; i < products.length ; i++){
        result.push(products[i].title)
      }
      //2.把数据输出到客户端
      res.json({code:200,message:'ok',data:result})
    }
  })
  
});

/* 热门饰品 */
router.get('/jewHot', function(req, res, next) {
  res.render('index', { title: 'Express' });
});
/* 最新上架 */
router.get('/jewNewList', function(req, res, next) {
  res.render('index', { title: 'Express' });
});
/* 最新求购 */
router.get('/jewNewBuy', function(req, res, next) {
  res.render('index', { title: 'Express' });
});

module.exports = router;
